<!-- 课程管理(模块) => 课程列表(菜单) => 台账操作列(课代表管理按钮) => 点开后展示此组件 -->
<template>
  <Dialog :title="t('course.assistant.title')" v-model="dialogVisible" width="900">
    <!-- 搜索 -->
    <ContentWrap>
      <el-form
        class="-mb-15px"
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="68px"
      >
        <el-form-item>
          <el-button type="primary" plain @click="handleSelectAssistant">
            <Icon icon="ep:user" />
            {{ t('assistant.addBtn') }}
          </el-button>
          <el-button type="success" plain @click="handleAddAssistant">
            <Icon icon="ep:plus" />
            {{ t('assistant.newBtn') }}
          </el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>
    <ContentWrap>
      <vxe-table
        id="mentor-select-table"
        ref="tableRef"
        v-loading="loading"
        :data="list"
        stripe
        :column-config="{ resizable: true }"
        :size="vxeTableSize"
      >
        <vxe-column :title="t('common.index')" type="seq" field="seq" width="60" align="center" />
        <vxe-column
          :title="t('assistant.fields.username')"
          field="username"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('assistant.fields.nickname')"
          field="nickname"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('systemUser.fields.mobile')"
          field="mobile"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('common.operate')"
          field="operator"
          align="center"
          show-header-overflow
          show-overflow
          fixed="right"
          width="80"
        >
          <template #default="scope">
            <div class="flex items-center justify-center">
              <el-button type="primary" link @click="handleDelete(scope.row.id)">
                {{ t('action.delete') }}
              </el-button>
            </div>
          </template>
        </vxe-column>
      </vxe-table>

      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </ContentWrap>
  </Dialog>

  <CourseAssistantSelect ref="courseAssistantSelectRef" @choose="resetQuery" />
  <AddCourseAssistant ref="addCourseAssistantRef" />
</template>

<script setup lang="ts">
import { useAppStore } from '@/store/modules/app'
import { CourseAdminRespVO } from '@/api/course/course'
import CourseAssistantSelect from './CourseAssistantSelect.vue'
import AddCourseAssistant from './AddCourseAssistant.vue'
import { AssistantApi, AssistantCourseRespVO } from '@/api/course/assistant'

/** 课程分配课代表 列表 */
defineOptions({ name: 'CourseAssistantView' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const dialogVisible = ref(false)

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref<AssistantCourseRespVO[]>([]) // 列表的数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  courseId: undefined
})

const queryFormRef = ref() // 搜索的表单

const appStore = useAppStore()
const vxeTableSize = computed(() => appStore.getCurrentSizeVxe)

/** 查询列表 */
const getList = async () => {
  queryParams.courseId = course.value.id
  loading.value = true
  try {
    const data = await AssistantApi.getAssistantPageByCourse(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields()
  handleQuery()
}

// 弹窗选择课代表
const courseAssistantSelectRef = ref<InstanceType<typeof CourseAssistantSelect>>()
const handleSelectAssistant = () => {
  courseAssistantSelectRef.value?.open(course.value)
}

// 创建课代表账号
const addCourseAssistantRef = ref<InstanceType<typeof AddCourseAssistant>>()
const handleAddAssistant = () => {
  addCourseAssistantRef.value?.open('create')
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await AssistantApi.deleteAssistant(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

const course = ref<CourseAdminRespVO>({} as CourseAdminRespVO)
const open = async (row: CourseAdminRespVO) => {
  course.value = row
  dialogVisible.value = true
  // 查询所有有教师角色的人
  getList()
}

const emits = defineEmits(['choose'])

defineExpose({ open })
</script>

<style scoped lang="scss"></style>
